<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\User as UserModel;
 
class User extends Controller
{
	public function login()
	{
		$user = session('user');
		if ($user && $user->id) {
			$this->redirect(url('index/index'));
		}
		return $this->fetch();
	}

	/**
	 * 前台注册
	 * @return [type] [description]
	 */
	public function register()
	{
		if (request()->isPost()) {
			$data = input('post.');
			if (!captcha_check($data['verify'])) {
				$this->error('验证码不正确！');
			}
			if ($data['password'] != $data['confirm']) {
				$this->error('两次密码不一致！');
			}
			$data['password'] = md5($data['password']);
			try{
				$user = new UserModel();
				$res = $user->allowField(true)->save($data);
			}catch(\Exception $e){
				$this->error($e->getMessage());
			}
			if($res) {
                $this->success('注册成功,请到个人中心完善资料',url('user/login'));
            }else{
                $this->error('注册失败');
            }
		} else{
			return $this->fetch();
		}
		
	}

	/**
	 * 检查用户是否存在
	 * @return [type] [description]
	 */
	public function checkuser()
	{
		if (request()->isAjax()) {
			$user = UserModel::getByUsername(input('post.username'));
			if (is_null($user)) {
				echo 0;
			} else {
				echo 1;
			}
		}
	}

	/**
	 * 登录检测
	 * @return [type] [description]
	 */
	public function logincheck()
	{
		if (!request()->isPost()) {
			$this->error('提交不合法');
		}
		$data = input('post.');
		$username = $data['username'];
		try {
			// 邮箱登录
			if (strpos($username,'@') !== false) {
				$user = model('User')->getByEmail($username);
			}
			 // 手机登录
			else if(preg_match("/^1[34578]{1}\d{9}$/", $username)){
				$user = model('User')->getByMobile($username);
			} else{
				$user = model('User')->getByUsername($username);
			}	
		} catch (\Exception $e) {
			$this->error($e->getMessage());
		}

		if (!$user || $user->status != 1 || $user->delete_time != null) {
			$this->error('该用户不存在或者被禁用');
		}

		if (md5($data['password']) != $user->password) {
			$this->error('密码不正确');
		}

		// 登录成功，更新数据
		UserModel::where('id',$user->id)->update(['last_login_time'=>time()]);
		// 把用户信息记录到session
		session('user',$user);
		$this->success('登录成功',url('index/index'));
	}

	//ajax登录验证 
	public function ajaxLogin()
	{
		if (!request()->isAjax()) {
			$this->error('请求不合法');
		}
		$data = input('post.');
		$username = $data['username'];
		try {
			// 邮箱登录
			if (strpos($username,'@') !== false) {
				$user = model('User')->getByEmail($username);
			}
			 // 手机登录
			else if(preg_match("/^1[34578]{1}\d{9}$/", $username)){
				$user = model('User')->getByMobile($username);
			} else{
				$user = model('User')->getByUsername($username);
			}	
		} catch (\Exception $e) {
			$this->error($e->getMessage());
		}
		if (!$user || $user->status != 1 || $user->delete_time != null){
			$this->result($_SERVER['HTTP_REFERER'], 0, '该用户不存在或者被禁用');
		}
		if (md5($data['password']) != $user->password) {
			$this->result($_SERVER['HTTP_REFERER'], 0, '密码不正确');
		}
		// 登录成功，更新数据
		UserModel::where('id',$user->id)->update(['last_login_time'=>time()]);
		// 把用户信息记录到session
		session('user',$user);
		$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
	}

	// 退出登录
	public function logout()
	{
		session(null);
		$this->redirect(url('user/login'));
	}
}